﻿/* ==============================================================================
 * 功能描述：GoodsItem  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/7 21:56:24
 * ==============================================================================*/

using System;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZZQFrameWork;

public class GoodsItem : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private BackpackPanel backpackPanel;

    /// <summary>
    /// 外框高亮
    /// </summary>
    private Image frameImage;

    /// <summary>
    /// 物品icon
    /// </summary>
    private Image goodsIconImage;

    /// <summary>
    /// 垃圾箱模式选择后显示勾
    /// </summary>
    private Image selectSureImage;

    /// <summary>
    /// 物品数量
    /// </summary>
    private Text counText;

    /// <summary>
    /// 是否选择该物品
    /// </summary>
    public bool isSelect = false;

    private GoodsVO data;

    public GoodsVO Data
    {
        get { return data; }
        set
        {
            data = value;
            UpdateShow();
        }
    }

    void Awake()
    {
        frameImage = transform.GetComponentByPath<Image>("Frame");
        goodsIconImage = transform.GetComponentByPath<Image>("GoodsIcon");
        frameImage = transform.GetComponentByPath<Image>("Frame");
        selectSureImage = transform.GetComponentByPath<Image>("Select_trash");
        counText = transform.GetComponentByPath<Text>("Count");
    }

    private void UpdateShow()
    {
        if (Data != null)
        {
            goodsIconImage.gameObject.SetActive(true);
            if (Data.goodsType == Enum_GoodsType.Equip)
            {
                goodsIconImage.sprite = backpackPanel.EquipAtlas.GetSprite(Data.icon);
                counText.gameObject.SetActive(false);
            }
            else
            {
                goodsIconImage.sprite = backpackPanel.GoodsAtlas.GetSprite(Data.icon);
                if (Data.count > 1)
                {
                    SetCountNull(true);
                    counText.text = Data.count.ToString();
                }
                else
                {
                    SetCountNull(false);
                }
            }
        }
        else
        {
            goodsIconImage.gameObject.SetActive(false);
            SetItemClicked(false);
            SetItemSelected(false);
            SetCountNull(false);
        }
    }

    public void SetBackpackPanel(BackpackPanel panel)
    {
        backpackPanel = panel;
    }

    public void SetItemClicked(bool clicked)
    {
        frameImage.gameObject.SetActive(clicked);
    }

    public void SetItemSelected(bool selected)
    {
        SetItemClicked(selected);
        selectSureImage.gameObject.SetActive(selected);
    }

    public void SetCountNull(bool isNull)
    {
        counText.gameObject.SetActive(isNull);
    }

    #region 物品拖拽

    public void OnBeginDrag(PointerEventData eventData)
    {

    }

    public void OnDrag(PointerEventData eventData)
    {
    }

    public void OnEndDrag(PointerEventData eventData)
    {
    }

    #endregion

}